//
//  LastLyView.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/4.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "LastLyView.h"
#import "ShoppingLastlyCollectionViewCell.h"
#import "StockGoodsContentViewController.h"

#import "MoreGoodsShowViewController.h"

#import <AlibcTradeBiz/AlibcTradeBiz.h>
#import <AlibcTradeSDK/AlibcTradeSDK.h>


static NSString *goodsCellID = @"goodsCellID";

@interface LastLyView ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (strong, nonatomic) UIView *headerV;
@property (strong, nonatomic) UIImageView *leftIV;
@property (strong, nonatomic) UIImageView *rightIV;
@property (strong, nonatomic) UILabel *headerTitle;
@property (strong, nonatomic) UILabel *desL;

@property (strong, nonatomic) UIView *contentV;
@property (strong, nonatomic) UIImageView *lastlyIV;
@property (strong, nonatomic) UIView *goodsView;
@property (strong, nonatomic) UIView *cornerView;
@property (strong, nonatomic) UIImageView *smallBackIV;
@property (strong, nonatomic) UILabel *loadMoreL;

@property (strong, nonatomic) UICollectionView *goodsCollectionView;

@property (assign, nonatomic) NSInteger controllerType;


@end

@implementation LastLyView

- (instancetype)initWithTitle:(NSString *)title andDes:(NSString *)desl withControllerType:(NSInteger)controllerType {
    
    self = [super init];
    
    if (self) {

        self.controllerType = controllerType;
        
        self.headerTitle.text = title;
        self.desL.text = desl;
        
        [self addSubview:self.headerV];
        [self.headerV addSubview:self.leftIV];
        [self.headerV addSubview:self.rightIV];
        [self.headerV addSubview:self.headerTitle];
        [self.headerV addSubview:self.desL];
        
        [self addSubview:self.contentV];
        [self.contentV addSubview:self.lastlyIV];
        [self.contentV addSubview:self.goodsView];
        [self.goodsView addSubview:self.cornerView];
        [self.cornerView addSubview:self.goodsCollectionView];
        [self.cornerView addSubview:self.smallBackIV];
        [self.cornerView addSubview:self.loadMoreL];

    }
    
    return self;
}

#pragma mark -更多最新点击
- (void)loadMoreGestureClick:(UIGestureRecognizer *)recognizer {
    
    NSLog(@"更多最新点击事件");
    
    if (self.controllerType == 0) {
        
        if (self.supportType == 0) {
            
            NSLog(@"购物===自营====更多最新点击事件");
            
            
        }else {
            
            NSLog(@"购物===非自营====更多最新点击事件");
            
        }
        
        MoreGoodsShowViewController *goodsVC = [[MoreGoodsShowViewController alloc] init];
        goodsVC.title = @"最新上架";
        goodsVC.controllerType = self.controllerType;
//        goodsVC.isSelfSupport = self.isSelfSupport;
        goodsVC.supportType = self.supportType;
        [self.parentVC.navigationController pushViewController:goodsVC animated:YES];
        
    }else {
        
        if (self.supportType == 0) {
            
            NSLog(@"进货====自营====更多最新点击事件");
            
        }else {
            
            NSLog(@"进货====非自营====更多最新点击事件");
            
        }
        
        MoreGoodsShowViewController *goodsVC = [[MoreGoodsShowViewController alloc] init];
        goodsVC.title = @"推荐进货";
//        goodsVC.isSelfSupport = self.isSelfSupport;
        goodsVC.supportType = self.supportType;
        goodsVC.controllerType = self.controllerType;
        [self.parentVC.navigationController pushViewController:goodsVC animated:YES];
    }

}


#pragma mark -delegate、datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 3;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ShoppingLastlyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:goodsCellID forIndexPath:indexPath];
    
    cell.backgroundColor = WhiteColor;
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (self.controllerType == 0) {
        
        if (self.supportType == 0) {
            
            NSLog(@"购物===自营====最新上架产品点击事件");
            ShoppingGoodsContentViewController *vc = [[ShoppingGoodsContentViewController alloc] init];
            [self.parentVC.navigationController pushViewController:vc animated:YES];
            
        }else {
            
            NSLog(@"购物===非自营====最新上架产品点击事件");

                // 优先拉起手淘：@"taobao_scheme" \\\ 天猫：tmall_scheme
//            [CommonAction sharedInstance].linkType = @"taobao_scheme";
//
//            [[CommonAction sharedInstance] goToTbGoodsDetailWith:self.parentVC itemDetailPage:@"41576306115"];

        }
        
    }else {
        //
        //        if (self.isSelfSupport) {
        //
        //            NSLog(@"进货====自营====最新上架产品点击事件");
        //
        //        }else {
        //
        //            NSLog(@"进货====非自营====最新上架产品点击事件");
        //
        //        }
        
        ShoppingGoodsContentViewController *vc = [[ShoppingGoodsContentViewController alloc] init];
        [self.parentVC.navigationController pushViewController:vc animated:YES];
        
//        StockGoodsContentViewController *vc = [[StockGoodsContentViewController alloc] init];
//        [self.parentVC.navigationController pushViewController:vc animated:YES];
        
    }

}


- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    __weak typeof(self) weakSelf = self;
    [self.headerV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.mas_left);
        make.right.mas_equalTo(weakSelf.mas_right);
        make.top.mas_equalTo(weakSelf.mas_top);
        make.height.mas_equalTo(jisuan(140));
    }];
    [self.headerTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.headerV.mas_centerX);
        make.top.mas_equalTo(weakSelf.headerV.mas_top).with.offset(jisuan(38));
        
    }];
    [self.leftIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(weakSelf.headerTitle.mas_centerY);
        make.right.mas_equalTo(weakSelf.headerTitle.mas_left).with.offset(-SmallMargin);
        make.width.mas_equalTo(jisuan(30));
        make.height.mas_equalTo(jisuan(24));
    }];
    [self.rightIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(weakSelf.headerTitle.mas_centerY);
        make.left.mas_equalTo(weakSelf.headerTitle.mas_right).with.offset(SmallMargin);
        make.width.mas_equalTo(jisuan(30));
        make.height.mas_equalTo(jisuan(24));
    }];
    [self.desL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.headerV.mas_centerX);
        make.bottom.mas_equalTo(weakSelf.headerV.mas_bottom).with.offset(-jisuan(30));
    }];
    
    [self.contentV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.mas_left);
        make.right.mas_equalTo(weakSelf.mas_right);
        make.top.mas_equalTo(weakSelf.headerV.mas_bottom);
        make.bottom.mas_equalTo(weakSelf.mas_bottom);
        
    }];
    [self.lastlyIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
//        make.centerX.mas_equalTo(weakSelf.contentV.mas_centerX);
        make.top.mas_equalTo(weakSelf.contentV.mas_top);
        make.left.mas_equalTo(weakSelf.contentV.mas_left);
        make.right.mas_equalTo(weakSelf.contentV.mas_right);
        
//        make.width.mas_equalTo(jisuan(750));
        make.height.mas_equalTo(jisuan(358));
        
    }];
    [self.goodsView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.contentV.mas_left).with.offset(Margin);
        make.right.mas_equalTo(weakSelf.contentV.mas_right);
//        make.top.mas_equalTo(weakSelf.lastlyIV.mas_bottom).with.offset(-jisuan(70));
        make.height.mas_equalTo(jisuan(321));
        make.bottom.mas_equalTo(weakSelf.contentV.mas_bottom);
        
    }];
    [self.cornerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.goodsView.mas_left).with.offset(0);
        make.right.mas_equalTo(weakSelf.goodsView.mas_right);
        make.top.mas_equalTo(weakSelf.goodsView.mas_top).with.offset(0);
//        make.height.mas_equalTo(jisuan(321)); // 301
        make.bottom.mas_equalTo(weakSelf.goodsView.mas_bottom);
        
    }];
    
    [self.goodsCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.cornerView.mas_left);
        make.top.mas_equalTo(weakSelf.cornerView.mas_top);
        make.right.mas_equalTo(weakSelf.cornerView.mas_right);
        make.bottom.mas_equalTo(weakSelf.cornerView.mas_bottom).with.offset(-jisuan(61));
        
    }];
    [self.loadMoreL mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerX.mas_equalTo(weakSelf.cornerView.mas_centerX).with.offset(-jisuan(10));
        make.bottom.mas_equalTo(weakSelf.cornerView.mas_bottom);
        make.height.mas_equalTo(jisuan(60));
        
    }];
    
    [self.smallBackIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.loadMoreL.mas_right).with.offset(jisuan(10));
        make.bottom.mas_equalTo(weakSelf.cornerView.mas_bottom).with.offset(-jisuan(20));
        make.width.mas_equalTo(jisuan(11));
        make.height.mas_equalTo(jisuan(20));
        
    }];
    
}

#pragma mark -lazy
- (UIView *)headerV {
    
    if (!_headerV) {
        
        _headerV = [[UIView alloc] init];
        _headerV.backgroundColor = WhiteColor;
    }
    
    return _headerV;
    
}

- (UIImageView *)leftIV {
    
    if (!_leftIV) {
        
        _leftIV = [[UIImageView alloc] init];
        _leftIV.contentMode = UIViewContentModeScaleAspectFit;
        _leftIV.image = [UIImage imageNamed:@"sectionHeaderLeft"];
    }
    
    return _leftIV;
}

- (UIImageView *)rightIV {
    
    if (!_rightIV) {
        
        _rightIV = [[UIImageView alloc] init];
        _rightIV.contentMode = UIViewContentModeScaleAspectFit;
        _rightIV.image = [UIImage imageNamed:@"sectionHeaderRight"];
        
    }
    
    return _rightIV;
}

- (UILabel *)headerTitle {
    
    if (!_headerTitle) {
        
        _headerTitle = [[UILabel alloc] init];
        _headerTitle.font = [UIFont fontWithName:@"Helvetica-Bold" size:jisuan(30)];
        _headerTitle.textAlignment = NSTextAlignmentCenter;
        _headerTitle.textColor = Color333333;
        _headerTitle.text = @"最新上架";
    }
    
    return _headerTitle;
}

- (UILabel *)desL {
    
    if (!_desL) {

        _desL = [[UILabel alloc] init];
        _desL.textAlignment = NSTextAlignmentCenter;
        _desL.textColor = Color999999;
        _desL.font = Font(20);
        _desL.text = @"新品上架，每天都是全新的";
        
    }
    
    return _desL;
    
}

- (UIView *)contentV {
    
    if (!_contentV) {
        
        _contentV = [[UIView alloc] init];
        
    }
    
    return _contentV;
    
}

- (UIImageView *)lastlyIV {
    
    if (!_lastlyIV) {
        
        _lastlyIV = [[UIImageView alloc] init];
        _lastlyIV.image = [UIImage imageNamed:@"shopLastlyBg"];
        _lastlyIV.contentMode = UIViewContentModeScaleAspectFit;
        _lastlyIV.backgroundColor = WhiteColor;
        
    }
    
    return _lastlyIV;
    
}

- (UIView *)goodsView {
    
    if (!_goodsView) {
        
        _goodsView = [[UIView alloc] init];
        _goodsView.backgroundColor = [UIColor clearColor];
        
        // Colorff2952
        _goodsView.layer.shadowColor = Colorb6b6b6.CGColor;
        _goodsView.layer.shadowOpacity = 0.35;
        _goodsView.layer.shadowRadius = jisuan(5);
        _goodsView.layer.shadowOffset = CGSizeMake(-3, 3);
        
    }
    
    return _goodsView;
    
}


- (UIView *)cornerView {
    
    if (!_cornerView) {
        
        _cornerView = [[UIView alloc] init];
        _cornerView.backgroundColor = WhiteColor;
        _cornerView.frame = CGRectMake(0, 0, SCREEN_WIDTH - Margin, jisuan(320));
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:_cornerView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:CGSizeMake(jisuan(10), jisuan(10))];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = _cornerView.bounds;
        //赋值
        maskLayer.path = path.CGPath;
        _cornerView.layer.mask = maskLayer;
        
    }
    
    return _cornerView;
    
}

- (UICollectionView *)goodsCollectionView {
    
    if (!_goodsCollectionView) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(jisuan(257), jisuan(259));
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.minimumLineSpacing = jisuan(1);
        flowLayout.minimumInteritemSpacing = jisuan(1);
        
        _goodsCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _goodsCollectionView.backgroundColor = Colordddddd;
        
        _goodsCollectionView.bounces = NO;
        
        _goodsCollectionView.delegate = self;
        _goodsCollectionView.dataSource = self;
        
        _goodsCollectionView.showsVerticalScrollIndicator = NO;
        _goodsCollectionView.showsHorizontalScrollIndicator = NO;
        
        [_goodsCollectionView registerClass:[ShoppingLastlyCollectionViewCell class] forCellWithReuseIdentifier:goodsCellID];
        
        if (@available(iOS 11.0, *)) {
            
            _goodsCollectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        }
        
    }
    
    return _goodsCollectionView;
    
}

- (UIImageView *)smallBackIV {
    
    if (!_smallBackIV) {
        
        _smallBackIV = [[UIImageView alloc] init];
        _smallBackIV.image = [UIImage imageNamed:@"smallBackBg1"];
        _smallBackIV.contentMode = UIViewContentModeScaleAspectFit;
        
    }
    
    return _smallBackIV;
}

- (UILabel *)loadMoreL {
    
    if (!_loadMoreL) {
        
        _loadMoreL = [[UILabel alloc] init];
        _loadMoreL.text = @"更多最新";
        _loadMoreL.textColor = Color1f89ff;
        _loadMoreL.font = Font(20);
        _loadMoreL.textAlignment = NSTextAlignmentRight;
        
        _loadMoreL.userInteractionEnabled = YES;
        UITapGestureRecognizer *loadMoreGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loadMoreGestureClick:)];
        [_loadMoreL addGestureRecognizer:loadMoreGesture];
        
    }
    
    return _loadMoreL;
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
